Imports System.IO
Imports System.Reflection
Imports System.Collections.Generic
Imports System.Linq
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Audio
Imports Microsoft.Xna.Framework.Content
Imports Microsoft.Xna.Framework.GamerServices
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Input
Imports Microsoft.Xna.Framework.Media
Imports Microsoft.Xna.Framework.Input.Touch

Class Spikes
	Inherits Tile
	Private Shared tileSequence As New List(Of Sequence)()
	'private RoomNew room;
	'public int switchButton = 0;
	Public elapsedTimeOpen As Single = 0
	'public float timeFall = 0.5f;
	Public timeOpen As Single = 6

	Public ReadOnly Property State() As Enumeration.StateTile
		Get
			Return tileState.Value().state
		End Get
	End Property


	Public Sub New(room As RoomNew, Content As ContentManager, tileType As Enumeration.TileType, state As Enumeration.StateTile, NextTileType__1 As Enumeration.TileType)
		MyBase.room = room

		nextTileType = NextTileType__1
		'this.switchButton = switchButton;
		Dim ax As New System.Xml.Serialization.XmlSerializer(tileSequence.[GetType]())

		Dim txtReader As Stream = Microsoft.Xna.Framework.TitleContainer.OpenStream(PrinceOfPersiaGame.CONFIG_PATH_CONTENT & PrinceOfPersiaGame.CONFIG_PATH_SEQUENCES & tileType.ToString().ToUpper() & "_sequence.xml")
		'TextReader txtReader = File.OpenText(PrinceOfPersiaGame.CONFIG_PATH_CONTENT + PrinceOfPersiaGame.CONFIG_PATH_SEQUENCES + tileType.ToString().ToUpper() + "_sequence.xml");

		tileSequence = DirectCast(ax.Deserialize(txtReader), List(Of Sequence))

		For Each s As Sequence In tileSequence
			s.Initialize(Content)
		Next

		'Search in the sequence the right type
		'return s.name == tileType.ToString().ToUpper();
		Dim result As Sequence = tileSequence.Find(Function(s As Sequence) s.name = state.ToString().ToUpper())

		If result IsNot Nothing Then
			'AMF to be adjust....
			result.frames(0).SetTexture(Content.Load(Of Texture2D)(PrinceOfPersiaGame.CONFIG_TILES & result.frames(0).value))

			collision = result.collision
			Texture = result.frames(0).texture
		End If
		Type = tileType


		'change statetile element
		tileState.Value().state = state
		tileAnimation.PlayAnimation(tileSequence, tileState.Value())
	End Sub

	Public Sub Normal()
		If tileState.Value().state = Enumeration.StateTile.normal Then
			Return
		End If

		tileState.Add(Enumeration.StateTile.normal)
		tileAnimation.PlayAnimation(tileSequence, tileState.Value())
	End Sub


	Public Sub Open()
		elapsedTimeOpen = 0

		If tileState.Value().state = Enumeration.StateTile.open Then
			Return
		End If
		If tileState.Value().state = Enumeration.StateTile.opened Then
			Return
		End If


		tileState.Add(Enumeration.StateTile.open)
		tileAnimation.PlayAnimation(tileSequence, tileState.Value())
	End Sub

	Public Sub Close()
		elapsedTimeOpen = timeOpen

		If tileState.Value().state = Enumeration.StateTile.close Then
			Return
		End If

		tileState.Add(Enumeration.StateTile.close)
		tileAnimation.PlayAnimation(tileSequence, tileState.Value())

	End Sub




End Class
